<template>
	<view class="choose-station-container">
		<scroll-view class="scroll-view" scroll-y>
			<view class="station-list">
				<view class="station-item" v-for="(item, index) in stationList" :key="index">
					<view class="station-item-title" v-if="item.list.length != 0">{{ item.value }}</view>
					<view class="station-item-content" v-if="item.list.length != 0">
						<view
							:class="'station ' + (stationItem.code == chooseStation.code ? 'active' : '')"
							@tap="chooseStationFun"
							:data-item="stationItem"
							v-for="(stationItem, stationIndex) in item.list"
							:key="stationIndex"
						>
							{{ stationItem.nameData }}
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
		<view class="common-btns-container">
			<view class="common-btn-container" hover-class="common-btn" @tap="submit">确认选择</view>
		</view>
	</view>
</template>

<script>
import { common } from '@/pagesA/utils/common.js';
const app = getApp();
export default {
	data() {
		return {
			projectUuid: '',
			stationList: [],
			chooseStation: {
				code: ''
			},
			stationItem: {
				code: '',
				nameData: ''
			},
			stationIndex: 0,
			unitType: ''
		};
	},
	onLoad(options) {
		let projectUuid = !options.projectUuid || options.projectUuid == 'undefined' || options.projectUuid == 'null' ? '' : options.projectUuid;
		let chooseStation = app.globalData.chooseStation || {};
		this.unitType = options.unitType || '';
		// this.setData({
		// 	projectUuid,
		// 	chooseStation
		// });
		this.projectUuid = projectUuid;
		this.chooseStation = chooseStation;
		if (this.projectUuid) {
			//扫项目码，加入项目
			this.getStation();
		} else {
			this.getRosterStation();
		}
	},
	onShow() {},
	methods: {
		handleStationList(list) {
			//处理职位列表
			let stationList = list;
			if (this.unitType) {
				//筛选职位列表
				for (let item of stationList) {
					if (item.typeCode == this.unitType) {
						stationList = [item];
						break;
					}
				}
			}
			for (let item of stationList) {
				for (let stationItem of item.list) {
					if (stationItem.name && stationItem.name.indexOf('-') > -1) {
						stationItem.nameData = stationItem.name.split('-')[1];
					} else {
						stationItem.nameData = stationItem.name;
					}
				}
			}
			this.stationList = stationList;
		},
		getStation() {
			let postData = {
				projectUuid: this.projectUuid
			};
			if (!postData.projectUuid) {
				delete postData.projectUuid;
			}
			common.apis.project.getStation(postData).then((res) => {
				if (!res.data) {
					return;
				}
				this.handleStationList(res.data);
			});
		},

		getRosterStation() {
			common.apis.roster.getStation().then((res) => {
				if (!res.data) {
					return;
				}
				this.handleStationList(res.data);
			});
		},
		chooseStationFun(e) {
			const item = e.currentTarget.dataset.item;
			// this.setData({
			// 	chooseStation: item
			// });
			this.chooseStation = item;
		},

		submit() {
			app.globalData.hasChooseStation = true;
			app.globalData.chooseStation = this.chooseStation;
			uni.navigateBack();
		}
	}
};
</script>
<style>
@import url('@/pagesA/assets/style/common.css');
.choose-station-container {
	display: flex;
	flex-direction: column;
	height: 100vh;
	background-color: #f6f7fa;
}

.scroll-view {
	flex: 1;
	height: 0;
}

.station-item {
	background-color: #fff;
}

.station-item .station-item-title {
	padding: 16rpx 32rpx;
	background: #f6f7fa;
	font-size: 28rpx;
	font-family: PingFangSC-Regular, PingFang SC;
	font-weight: 400;
	color: #949dbf;
}

.station-item .station-item-content {
	display: flex;
	align-items: center;
	flex-wrap: wrap;
	padding: 0 32rpx 24rpx;
}

.station-item-content .station {
	font-size: 28rpx;
	font-family: PingFangSC-Regular, PingFang SC;
	font-weight: 400;
	color: #4b5680;
	padding: 8rpx 24rpx;
	border: 1rpx solid #949dbf;
	margin-right: 32rpx;
	margin-top: 24rpx;
	border-radius: 8rpx;
}

.station-item-content .active {
	color: #ffffff;
	background-color: #3f64fa;
}
</style>
